{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {
    "toc": true
   },
   "source": [
    "<h1>目录<span class=\"tocSkip\"></span></h1>\n",
    "<div class=\"toc\"><ul class=\"toc-item\"><li><span><a href=\"#需求\" data-toc-modified-id=\"需求-1\"><span class=\"toc-item-num\">1&nbsp;&nbsp;</span>需求</a></span></li><li><span><a href=\"#思想\" data-toc-modified-id=\"思想-2\"><span class=\"toc-item-num\">2&nbsp;&nbsp;</span>思想</a></span></li><li><span><a href=\"#实现\" data-toc-modified-id=\"实现-3\"><span class=\"toc-item-num\">3&nbsp;&nbsp;</span>实现</a></span></li></ul></div>"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 需求\n",
    "```\n",
    "给定一个链表，判断链表中是否有环。\n",
    "\n",
    "进阶：\n",
    "你能否不使用额外空间解决此题？\n",
    "```"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 思想\n",
    "1. 使用跨步法\n",
    "1. 使slow,fast都'指向'head\n",
    "1. 在链表（假设无环）的一趟while循环中，slow每次跨一步，fast每次跨两部\n",
    "1. 如果有一次slow,fast相等，则肯定存在环，否则不是环形链表"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 实现"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "def hasCycle(self, head):\n",
    "    fast = slow = head\n",
    "    while fast and fast.next:\n",
    "        fast = fast.next.next\n",
    "        slow = slow.next\n",
    "        if slow == fast:\n",
    "            return True\n",
    "    return False"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "base",
   "language": "python",
   "name": "base"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.5"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": true,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "目录",
   "title_sidebar": "大纲",
   "toc_cell": true,
   "toc_position": {},
   "toc_section_display": true,
   "toc_window_display": false
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
